<template>
    <nav 
        v-if="totalPages > 1" 
        class="pagination-container"
        aria-label="页面导航"
    >
        <ul class="pagination mb-0">
            <!-- 上一页按钮 -->
            <li class="page-item" :class="{ disabled: isFirstPage }">
                <button 
                    class="page-link"
                    @click="prevPage"
                    :disabled="isFirstPage"
                    title="上一页"
                >
                    <i class="bi bi-chevron-left"></i>
                    上一页
                </button>
            </li>

            <!-- 首页 -->
            <li class="page-item" :class="{ active: currentPage === 1 }">
                <button 
                    class="page-link"
                    @click="goToPage(1)"
                    :disabled="currentPage === 1"
                >
                    1
                </button>
            </li>

            <!-- 省略号（前） -->
            <li v-if="showLeftEllipsis" class="page-item disabled">
                <span class="page-link">...</span>
            </li>

            <!-- 中间页码 -->
            <li 
                v-for="page in middlePages" 
                :key="page"
                class="page-item"
                :class="{ active: currentPage === page }"
            >
                <button 
                    class="page-link"
                    @click="goToPage(page)"
                    :disabled="currentPage === page"
                >
                    {{ page }}
                </button>
            </li>

            <!-- 省略号（后） -->
            <li v-if="showRightEllipsis" class="page-item disabled">
                <span class="page-link">...</span>
            </li>

            <!-- 末页 -->
            <li 
                v-if="totalPages > 1"
                class="page-item"
                :class="{ active: currentPage === totalPages }"
            >
                <button 
                    class="page-link"
                    @click="goToPage(totalPages)"
                    :disabled="currentPage === totalPages"
                >
                    {{ totalPages }}
                </button>
            </li>

            <!-- 下一页按钮 -->
            <li class="page-item" :class="{ disabled: isLastPage }">
                <button 
                    class="page-link"
                    @click="nextPage"
                    :disabled="isLastPage"
                    title="下一页"
                >
                    下一页
                    <i class="bi bi-chevron-right"></i>
                </button>
            </li>
        </ul>

        <!-- 页面信息和快速跳转 -->
        <div class="pagination-info">
            <span class="info-text">
                第 {{ currentPage }} / {{ totalPages }} 页，共 {{ pageInfo.total }} 条
            </span>
            
            <div class="page-jumper">
                <label for="page-input">跳转至</label>
                <input 
                    id="page-input"
                    v-model.number="jumpPage"
                    type="number"
                    min="1"
                    :max="totalPages"
                    @keyup.enter="handleJump"
                    placeholder="页码"
                >
                <button 
                    class="btn btn-sm btn-primary"
                    @click="handleJump"
                    :disabled="!jumpPage || jumpPage < 1 || jumpPage > totalPages"
                >
                    跳转
                </button>
            </div>
        </div>
    </nav>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

interface Props {
    currentPage: number
    totalPages: number
    pageInfo: {
        start: number
        end: number
        total: number
        currentPage: number
        totalPages: number
        pageSize: number
    }
    isFirstPage: boolean
    isLastPage: boolean
    maxPagesToShow?: number
}

const props = withDefaults(defineProps<Props>(), {
    maxPagesToShow: 5
})

const emit = defineEmits<{
    'go-to-page': [page: number]
    'next-page': []
    'prev-page': []
}>()

const jumpPage = ref<number>()

// 计算中间显示的页码
const middlePages = computed(() => {
    const pages: number[] = []
    const { currentPage, totalPages, maxPagesToShow } = props

    if (totalPages <= maxPagesToShow + 2) {
        // 如果总页数较少，显示所有中间页
        for (let i = 2; i < totalPages; i++) {
            pages.push(i)
        }
    } else {
        // 计算显示的中间页码范围
        const halfShow = Math.floor((maxPagesToShow - 1) / 2)
        let start = Math.max(2, currentPage - halfShow)
        let end = Math.min(totalPages - 1, currentPage + halfShow)

        // 调整范围，确保显示足够的页码
        if (start === 2) {
            end = Math.min(totalPages - 1, start + maxPagesToShow - 2)
        } else if (end === totalPages - 1) {
            start = Math.max(2, end - maxPagesToShow + 2)
        }

        for (let i = start; i <= end; i++) {
            pages.push(i)
        }
    }

    return pages
})

// 是否显示前省略号
const showLeftEllipsis = computed(() => {
    return !middlePages.value.includes(2) && middlePages.value.length > 0
})

// 是否显示后省略号
const showRightEllipsis = computed(() => {
    const maxMiddlePage = middlePages.value[middlePages.value.length - 1]
    return maxMiddlePage && maxMiddlePage < props.totalPages - 1
})

// 下一页
const nextPage = () => {
    if (!props.isLastPage) {
        emit('go-to-page', props.currentPage + 1)
        emit('next-page')
    }
}

// 上一页
const prevPage = () => {
    if (!props.isFirstPage) {
        emit('go-to-page', props.currentPage - 1)
        emit('prev-page')
    }
}

// 跳转到指定页
const goToPage = (page: number) => {
    if (page !== props.currentPage) {
        emit('go-to-page', page)
    }
}

// 处理跳转
const handleJump = () => {
    if (jumpPage.value && jumpPage.value >= 1 && jumpPage.value <= props.totalPages) {
        goToPage(jumpPage.value)
        jumpPage.value = undefined
    }
}
</script>

<style scoped>
.pagination-container {
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin-top: 20px;
    padding: 15px;
    background-color: #f8f9fa;
    border-radius: 8px;
}

.pagination {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    justify-content: center;
}

.page-item {
    list-style: none;
}

.page-link {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 0.5rem 0.75rem;
    border: 1px solid #dee2e6;
    background-color: white;
    color: #0d6efd;
    text-decoration: none;
    cursor: pointer;
    border-radius: 4px;
    transition: all 0.2s;
    font-size: 0.875rem;
    font-weight: 500;
}

.page-link:hover:not(:disabled) {
    background-color: #0d6efd;
    color: white;
    border-color: #0d6efd;
    transform: translateY(-2px);
}

.page-item.active .page-link {
    background-color: #0d6efd;
    color: white;
    border-color: #0d6efd;
    cursor: default;
}

.page-item.disabled .page-link,
.page-link:disabled {
    color: #6c757d;
    background-color: #e9ecef;
    border-color: #dee2e6;
    cursor: not-allowed;
    opacity: 0.5;
}

.pagination-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
    font-size: 0.875rem;
    color: #6c757d;
}

.info-text {
    font-weight: 500;
}

.page-jumper {
    display: flex;
    align-items: center;
    gap: 8px;
}

.page-jumper label {
    margin: 0;
    font-weight: 500;
}

.page-jumper input {
    width: 50px;
    padding: 0.375rem 0.5rem;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    font-size: 0.875rem;
}

.page-jumper input:focus {
    outline: none;
    border-color: #0d6efd;
    box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
}

/* 响应式设计 */
@media (max-width: 768px) {
    .pagination-container {
        gap: 10px;
        padding: 10px;
    }

    .pagination {
        gap: 2px;
    }

    .page-link {
        padding: 0.4rem 0.6rem;
        font-size: 0.75rem;
    }

    .pagination-info {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .page-jumper {
        width: 100%;
    }

    .page-jumper input {
        flex: 1;
    }
}

/* 隐藏 input 的数字控制按钮 */
.page-jumper input::-webkit-outer-spin-button,
.page-jumper input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.page-jumper input[type='number'] {
    appearance: textfield;
    -moz-appearance: textfield;
}
</style>
